/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class UDPStreamSocketCommunicator.
//
// UDPStreamSocketCommunicator uses to send/receive UDP messages with a
// dynamic buffer.
// This UDP uses stream pattern.
//

#include <string.h>

#include "udp_stream_socket_communicator.h"
#include "packet_define.h"
#include "logging.h"

namespace amz {

UDPStreamSocketCommunicator::UDPStreamSocketCommunicator(
         const sockaddr_in &address, int len, UDPSocket *udp_socket,
         CompressType compress, EncryptType encrypt, int flag)
         : UDPSocketCommunicator(compress, encrypt, address, len, udp_socket),
           send_buffer_(packet::kMaxPacketSize), receive_buffer_(packet::kMaxPacketSize) {}

UDPStreamSocketCommunicator::~UDPStreamSocketCommunicator() {}

int UDPStreamSocketCommunicator::Send() {
  int send_total_size = 0, send_size = 0;
  // Loop send, each send is a udp packet.
  while(true) {
    uint32 read_size = this->send_buffer_.ReadSize();
    if(read_size == 0) {
      break;
    }
    read_size = read_size < packet::kMTU_UDP ?
           read_size : packet::kMTU_UDP;
    send_size = this->udp_socket_->Send(this->send_buffer_.ReadPointer(), read_size,
           (const struct sockaddr *)&this->address_in4_, sizeof(struct sockaddr), this->flag_);
    if(send_size < 0) {
      break;
    }
    this->send_buffer_.ReadShift(send_size);
    send_total_size += send_size;
  }
  return send_total_size;
}

int UDPStreamSocketCommunicator::Send(const char *message, int size) {
  // Calculate packet size, allocate, packet.
  int need_size = this->packet_.CalculatePacketSize(size);
  if(this->send_buffer_.PreWrite(need_size) == false) {
    LOG(ERROR) << "Failed to Send, send_buffer_ size is not enough!";
    return -1;
  }
  int packet_size = this->packet_.Packet(message, size, this->send_buffer_.WritePointer(),
         this->send_buffer_.WriteSize());
  if(packet_size <= 0) {
    return packet_size;
  }
  this->send_buffer_.WriteShift(packet_size);
  return size;
}

int UDPStreamSocketCommunicator::PushReceive(char *stream, int size) {
  if(this->receive_buffer_.PreWrite(size) == false) {
    LOG(ERROR) << "Failed to PushReceive, receive_buffer_ size is not enough!";
    return -1;
  }
  memcpy(this->receive_buffer_.WritePointer(), stream, size);
  this->receive_buffer_.WriteShift(size);
  return size;
}

int UDPStreamSocketCommunicator::Receive(char *message, int max_size) {
  // Unpacket a message.
  int read_size = this->packet_.Unpacket(this->receive_buffer_.ReadPointer(),
         this->receive_buffer_.ReadSize(), message, max_size);
  if(read_size > 0) {
    this->receive_buffer_.ReadShift(read_size);
  }
  return read_size;
}

}  // namespace amz

